package com.shuai.jit.controller;

import com.shuai.jit.entity.R;
import com.shuai.jit.entity.user.User;
import com.shuai.jit.entity.vo.VoUser;
import com.shuai.jit.service.impl.LoginServiceImpl;
import com.shuai.jit.service.impl.UserServiceImpl;
import com.shuai.jit.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    LoginServiceImpl loginService;

    @Autowired
    JwtUtil jwtUtil;


    @ApiOperation("根据Id来获取用户信息")
    @RequestMapping("/getInfoById")
    public R getUserInfo(String userId){
        log.info("getUserInfo"+userId);
        User user= userService.getUserInfo(userId);
        if(user!=null){
            return R.ok().data("user",user);
        }else {
            return R.error().data("user","查无此人");
        }
    }


    @ApiOperation("根据token，来获取用户信息")
    @RequestMapping("/getInfoByToken")
    public R getInfo( String token){

        if(token==null|| token.equals("") ||!jwtUtil.verify(token)){
            return R.error().data("user","对不起token错误");
        }
        VoUser voUser=loginService.getInfo(jwtUtil.getClaim(token,"username"));
        Map<String,Object> map=new HashMap<>();
        log.info(voUser.toString());
        if(voUser!=null){
            map.put("user",voUser);
            return R.ok().data(map);
        }else {
            map.put("user","null");
            return R.error().data(map);
        }
    }

    @ApiOperation("更新用户信息")
    @RequestMapping("/updateInfo")
    public R updateInfo(@RequestBody User user){
        log.info(user.toString());
        Integer result=userService.updateUser(user);
        log.info("更新用户"+result);
        if(result>0){
            // 重新签发token
            return R.ok().data("user",jwtUtil.sign(user.getUsername()));
        }else if(result==-2){
            return R.error().data("user","电话号码已经有了");
        }else {
            return R.error().data("user","更新失败");
        }
    }

    @ApiOperation("更新用户密码")
    @RequestMapping("/updatePassword")
    public R updatePassword(@RequestBody User user){
        int result=userService.updatePassword(user);

        if(result>0){
            return R.ok().data("user","密码更新成功");
        }else {
            return R.error().data("user","对不起密码更新失败");

        }
    }

    @ApiOperation("获取所有用户信息")
    @RequestMapping("/getAllUser")
    public R getAllUser(Integer current){
        if(current==null){
            return R.error().data("user","页数出现差错，为空");
        }else {
            return  R.ok().data("user",userService.getAllUser(current));
        }
    }

    @ApiOperation("管理员添加用户")
    @RequestMapping("/addUser")
    public R addUser(@RequestBody User user){
        Integer result = userService.addUser(user);
        if(result>=1){
            return R.ok().data("user","添加用户成功");
        }else if(result==-1){

            return R.error().data("register","手机号或者用户名已经存在");
        }else if(result==-2){
            return R.error().data("register","对不起验证码错误");
        }
        else {
            return R.error().data("register","添加用户失败");
        }
    }

}